package com.example.wacoffee_user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.wacoffee_user.pojo.dto.rechargeDTO;
import com.example.wacoffee_user.pojo.entity.User;
import com.example.wacoffee_user.pojo.entity.coupon;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface MeMapper extends BaseMapper<User> {
    @Select("select username,image,balance,point,exp,level from user where id = #{currentId}")
    User getMe(Integer currentId);

    @Insert("insert into recharge_order (user_id,amount,create_time) values (#{currentId},#{amount},#{now}) ")
    void addRechargeOrder(double amount, Integer currentId, LocalDateTime now);

    @Select("select id,amount,create_time from recharge_order where user_id = #{currentId} order by create_time desc ")
    List<rechargeDTO> getRechargeOrder(Integer currentId);


    void updateInformationById(User user);

    @Select("select count(id) from coupon_order where user_id=#{currentId} and status=1")
    Integer getCouponCount(Integer currentId);

    @Select("select c.id,c.name,c.value,c.min_amount,c.start_time,c.end_time,co.id as uid from coupon c left join coupon_order co on c.id=co.coupon_id where co.status=1  " )
    List<coupon> getMyCoupon(Integer currentId);

    @Update("update user set exp=exp-1000, level=level+1 where id=#{currentId} and exp>=1000")
    void checkAndUpdateLevel(Integer currentId);

    @Insert("insert into coupon_order (user_id,coupon_id) values (#{currentId},#{couponId})")
    void addCoupon(Integer currentId, int couponId);
}
